package cn.com.blueInfo.bpm.system.service.impl;

import cn.com.blueInfo.bpm.system.convert.DictConvert;
import cn.com.blueInfo.bpm.system.entity.Dict;
import cn.com.blueInfo.bpm.system.repository.DictRepository;
import cn.com.blueInfo.bpm.system.request.DictDTO;
import cn.com.blueInfo.bpm.system.response.DictVO;
import cn.com.blueInfo.bpm.system.service.DictService;
import cn.com.blueInfo.core.util.SnowflakeIdGenerator;
import cn.com.blueInfo.utils.result.ResultInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 枚举表 业务服务实现类
 * @author suxch
 * @since 2025-09-01 19:17:41
 */
@Log4j2
@Service
public class DictServiceImpl implements DictService {

    @Autowired
    private DictRepository dictRepository;

    @Autowired
    private SnowflakeIdGenerator snowflakeIdGenerator;

    @Autowired
    private DictConvert dictConvert;

    /**
     * 新增枚举
     * @param dictDTO 枚举参数
     */
    @Override
    public ResultInfo<Integer> addDict(DictDTO dictDTO) {
        Dict dict = dictConvert.dtoToEntity(dictDTO);
        dict.setId(snowflakeIdGenerator.nextId());
        Integer addSize = dictRepository.createDict(dict);
        return ResultInfo.<Integer>builder().data(addSize).message("枚举成功新增 {} 条").build();
    }

    /**
     * 批量新增枚举
     * @param dictDTOList 枚举参数列表
     */
    @Override
    public ResultInfo<Integer> batchAddDict(List<DictDTO> dictDTOList) {
        List<Dict> dictList = dictConvert.dtoToEntity(dictDTOList);
        for (Dict dict : dictList) dict.setId(snowflakeIdGenerator.nextId());
        Integer addSize = dictRepository.batchCreateDict(dictList);
        return ResultInfo.<Integer>builder().data(addSize).message("枚举成功新增 {} 条").build();
    }

    /**
     * 删除枚举
     * @param dictId 枚举ID
     */
    @Override
    public ResultInfo<Integer> removeDict(Long dictId) {
        Integer removeSize = dictRepository.deleteDict(dictId);
        return ResultInfo.<Integer>builder().data(removeSize).message("枚举成功删除 {} 条").build();
    }

    /**
     * 批量删除枚举
     *
     * @param dictIdList 枚举ID列表
     */
    @Override
    public ResultInfo<Integer> batchRemoveDict(List<Long> dictIdList) {
        return null;
    }

    /**
     * 更新部分枚举
     * @param dictUpdatePartialDTO 枚举参数
     */
    @Override
    public ResultInfo<Integer> modifyDictPartial(DictDTO.UpdatePartialDTO dictUpdatePartialDTO) {
        Dict dict = dictConvert.dtoToEntity(dictUpdatePartialDTO);
        Integer modifySize = dictRepository.updateDictPartial(dict);
        return ResultInfo.<Integer>builder().data(modifySize).message("枚举成功更新 {} 条").build();
    }

    /**
     * 更新全部枚举
     * @param dictDTO 枚举参数
     */
    @Override
    public ResultInfo<Integer> modifyDict(DictDTO dictDTO) {
        Dict dict = dictConvert.dtoToEntity(dictDTO);
        Integer modifySize = dictRepository.updateDict(dict);
        return ResultInfo.<Integer>builder().data(modifySize).message("枚举成功更新 {} 条").build();
    }

    /**
     * 查询一条枚举
     * @param dictId 枚举ID
     */
    @Override
    public ResultInfo<DictVO> queryDictById(Long dictId) {
        Dict dict = dictRepository.getDictById(dictId);
        DictVO dictVO = dictConvert.entityToVO(dict);
        return ResultInfo.<DictVO>builder().data(dictVO).message("枚举查询成功").build();
    }

    /**
     * 分页查询枚举
     * @param dictQueryDTO 枚举参数
     */
    @Override
    public ResultInfo<Page<DictVO>> queryDictPage(DictDTO.QueryDTO dictQueryDTO) {
        Page<Dict> dictPage = dictRepository.getDictPage(dictQueryDTO);
        Page<DictVO> resultData = dictConvert.convertPage(dictPage);
        return ResultInfo.<Page<DictVO>>builder()
                .data(resultData)
                .message("枚举分页查询成功，共 {} 条", resultData.getRecords().size())
                .build();
    }

    /**
     * 根据条件查询 枚举 数量
     * @param dictQueryDTO 枚举参数
     */
    @Override
    public ResultInfo<Long> queryDictCount(DictDTO.QueryDTO dictQueryDTO) {
        Long resultCount = dictRepository.getDictCount(dictQueryDTO);
        return ResultInfo.<Long>builder()
                .data(resultCount)
                .message("枚举数量查询成功，共 {} 条", resultCount)
                .build();
    }

    /**
     * 根据条件查询 枚举
     * @param dictQueryDTO 枚举参数
     */
    @Override
    public ResultInfo<List<DictVO>> queryDictList(DictDTO.QueryDTO dictQueryDTO) {
        List<Dict> dictList = dictRepository.getDictList(dictQueryDTO);
        List<DictVO> resultData = dictConvert.entityListToVoList(dictList);
        return ResultInfo.<List<DictVO>>builder()
                .data(resultData)
                .message("枚举分页查询成功，共 {} 条", resultData.size())
                .build();
    }
}
